package com.uetoken.cn.activity;

import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.uetoken.cn.R;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.base.BaseFragment;
import com.uetoken.cn.fragment.IsShipmentFragment;
import com.uetoken.cn.fragment.NotShipmentFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShipmentActivity extends BaseActivity {

    private static final String TAB_NOT_SHIPMENT = NotShipmentFragment.class.getName();

    private static final String TAB_IS_SHIPMENT = IsShipmentFragment.class.getName();

    @BindView(R.id.topbar)
    QMUITopBar mTopbar;
    @BindView(R.id.notBoundBtn)
    TextView mNotBoundBtn;
    @BindView(R.id.isBoundBtn)
    TextView mIsBoundBtn;

    private NotShipmentFragment mNotShipmentFragment;
    private IsShipmentFragment mIsShipmentFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shipment;
    }

    @Override
    public void initView() {
        // 沉浸式状态栏
        QMUIStatusBarHelper.translucent(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            QMUIStatusBarHelper.setStatusBarLightMode(this);
        }
        ButterKnife.bind(this);

        initTopBar();

        mNotShipmentFragment = new NotShipmentFragment();
        mIsShipmentFragment = new IsShipmentFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.rootLayout, mNotShipmentFragment).commit();
    }

    @Override
    public void initData() {
    }

    @Override
    public void init() {

    }

    private void initTopBar() {
        mTopbar.setTitle(R.string.purchase_details_title);
        mTopbar.addLeftImageButton(R.drawable.ic_back, R.id.qmui_topbar_item_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick({R.id.notBoundBtn, R.id.isBoundBtn})
    public void onViewClicked(View view) {
        LogUtils.dTag("onViewClicked===", "onViewClicked===");
        switch (view.getId()) {
            case R.id.notBoundBtn:
                mNotBoundBtn.setBackgroundColor(ContextCompat.getColor(this, R.color.color_app_theme));
                mIsBoundBtn.setBackgroundColor(ContextCompat.getColor(this, R.color.durban_White));
                mNotBoundBtn.setTextColor(ContextCompat.getColor(this, R.color.durban_White));
                mIsBoundBtn.setTextColor(ContextCompat.getColor(this, R.color.app_color_common_black));
                removeFragment(mIsShipmentFragment);
                startFragment(mNotShipmentFragment, TAB_NOT_SHIPMENT);
                break;
            case R.id.isBoundBtn:
                mNotBoundBtn.setBackgroundColor(ContextCompat.getColor(this, R.color.durban_White));
                mIsBoundBtn.setBackgroundColor(ContextCompat.getColor(this, R.color.color_app_theme));
                mNotBoundBtn.setTextColor(ContextCompat.getColor(this, R.color.app_color_common_black));
                mIsBoundBtn.setTextColor(ContextCompat.getColor(this, R.color.durban_White));
                removeFragment(mNotShipmentFragment);
                startFragment(mIsShipmentFragment, TAB_IS_SHIPMENT);
                break;
        }
    }

    public int startFragment(BaseFragment fragment, String tab) {
        return getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.rootLayout, fragment, tab)
                .addToBackStack(tab)
                .commit();
    }

    private void removeFragment(BaseFragment fragment) {
        getSupportFragmentManager()
                .beginTransaction().remove(fragment).commit();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
//        LogUtils.dTag("onBackPressed==", "onBackPressed==");
    }
}
